home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / libtiff / tif_tile.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  5KB  |  202 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_tile.c,v 1.9 92/02/10 19:06:47 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * Tiled Image Support Routines.
  33.  */
  34. #include "tiffioP.h"
  35.  
  36. /*
  37.  * Compute which tile an (x,y,z,s) value is in.
  38.  */
  39. u_int
  40. TIFFComputeTile(tif, x, y, s, z)
  41.     TIFF *tif;
  42.     u_long x, y, z;
  43.     u_int s;
  44. {
  45.     TIFFDirectory *td = &tif->tif_dir;
  46.     u_long dx = td->td_tilewidth;
  47.     u_long dy = td->td_tilelength;
  48.     u_long dz = td->td_tiledepth;
  49.     u_int tile = 1;
  50.  
  51.     if (td->td_imagedepth == 1)
  52.         z = 0;
  53.     if (dx == (u_long) -1)
  54.         dx = td->td_imagewidth;
  55.     if (dy == (u_long) -1)
  56.         dy = td->td_imagelength;
  57.     if (dz == (u_long) -1)
  58.         dz = td->td_imagedepth;
  59.     if (dx != 0 && dy != 0 && dz != 0) {
  60.         u_int xpt = howmany(td->td_imagewidth, dx); 
  61.         u_int ypt = howmany(td->td_imagelength, dy); 
  62.         u_int zpt = howmany(td->td_imagedepth, dz); 
  63.  
  64.         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
  65.             tile = (xpt*ypt*zpt)*s +
  66.                  (xpt*ypt)*(z/dz) +
  67.                  xpt*(y/dy) +
  68.                  x/dx;
  69.         else
  70.             tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx + s;
  71.     }
  72.     return (tile);
  73. }
  74.  
  75. /*
  76.  * Check an (x,y,z,s) coordinate
  77.  * against the image bounds.
  78.  */
  79. TIFFCheckTile(tif, x, y, z, s)
  80.     TIFF *tif;
  81.     u_long x, y, z;
  82.     u_int s;
  83. {
  84.     TIFFDirectory *td = &tif->tif_dir;
  85.  
  86.     if (x >= td->td_imagewidth) {
  87.         TIFFError(tif->tif_name, "Col %d out of range, max %d",
  88.             x, td->td_imagewidth);
  89.         return (0);
  90.     }
  91.     if (y >= td->td_imagelength) {
  92.         TIFFError(tif->tif_name, "Row %d out of range, max %d",
  93.             y, td->td_imagelength);
  94.         return (0);
  95.     }
  96.     if (z >= td->td_imagedepth) {
  97.         TIFFError(tif->tif_name, "Depth %d out of range, max %d",
  98.             z, td->td_imagedepth);
  99.         return (0);
  100.     }
  101.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  102.         s >= td->td_samplesperpixel) {
  103.         TIFFError(tif->tif_name, "Sample %d out of range, max %d",
  104.             s, td->td_samplesperpixel);
  105.         return (0);
  106.     }
  107.     return (1);
  108. }
  109.  
  110. /*
  111.  * Compute how many tiles are in an image.
  112.  */
  113. u_int
  114. TIFFNumberOfTiles(tif)
  115.     TIFF *tif;
  116. {
  117.     TIFFDirectory *td = &tif->tif_dir;
  118.     u_long dx = td->td_tilewidth;
  119.     u_long dy = td->td_tilelength;
  120.     u_long dz = td->td_tiledepth;
  121.     u_int ntiles;
  122.  
  123.     if (dx == (u_long) -1)
  124.         dx = td->td_imagewidth;
  125.     if (dy == (u_long) -1)
  126.         dy = td->td_imagelength;
  127.     if (dz == (u_long) -1)
  128.         dz = td->td_imagedepth;
  129.     ntiles = (dx != 0 && dy != 0 && dz != 0) ?
  130.         (howmany(td->td_imagewidth, dx) * howmany(td->td_imagelength, dy) *
  131.         howmany(td->td_imagedepth, dz)) :
  132.         0;
  133.     return (ntiles);
  134. }
  135.  
  136. /*
  137.  * Compute the # bytes in each row of a tile.
  138.  */
  139. u_long
  140. TIFFTileRowSize(tif)
  141.     TIFF *tif;
  142. {
  143.     TIFFDirectory *td = &tif->tif_dir;
  144.     u_long rowsize;
  145.     
  146.     if (td->td_tilelength == 0 || td->td_tilewidth == 0)
  147.         return (0);
  148.     rowsize = td->td_bitspersample * td->td_tilewidth;
  149.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  150.         rowsize *= td->td_samplesperpixel;
  151.     return (howmany(rowsize, 8));
  152. }
  153.  
  154. /*
  155.  * Compute the # bytes in a variable length, row-aligned tile.
  156.  */
  157. u_long
  158. TIFFVTileSize(tif, nrows)
  159.     TIFF *tif;
  160.     u_long nrows;
  161. {
  162.     TIFFDirectory *td = &tif->tif_dir;
  163.     u_long tilesize;
  164.  
  165.     if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  166.         td->td_tiledepth == 0)
  167.         return (0);
  168. #ifdef YCBCR_SUPPORT
  169.     if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  170.         td->td_photometric == PHOTOMETRIC_YCBCR) {
  171.         /*
  172.          * Packed YCbCr data contain one Cb+Cr for every
  173.          * HorizontalSampling*VerticalSampling Y values.
  174.          * Must also roundup width and height when calculating
  175.          * since images that are not a multiple of the
  176.          * horizontal/vertical subsampling area include
  177.          * YCbCr data for the extended image.
  178.          */
  179.         u_long w =
  180.             roundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
  181.         u_long rowsize = howmany(w*td->td_bitspersample, 8);
  182.         u_long samplingarea =
  183.             td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  184.         nrows = roundup(nrows, td->td_ycbcrsubsampling[1]);
  185.         /* NB: don't need howmany here 'cuz everything is rounded */
  186.         tilesize = nrows*rowsize + 2*(nrows*rowsize / samplingarea);
  187.     } else
  188. #endif
  189.         tilesize = nrows * TIFFTileRowSize(tif);
  190.     return (tilesize * td->td_tiledepth);
  191. }
  192.  
  193. /*
  194.  * Compute the # bytes in a row-aligned tile.
  195.  */
  196. u_long
  197. TIFFTileSize(tif)
  198.     TIFF *tif;
  199. {
  200.     return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
  201. }
  202.